﻿/**
* ...
* @author DefaultUser (Tools -> Custom Arguments...)
*/
package com.xuanque.mahjongDice.core {
	
	import com.xuanque.mahjongDice.util.ArrayUtilities;
	
	public class MahJongDice{
		
		private var pai_array:Array;
		private var pai_length:uint;
		private var range_array:Array;//归类
		
		private var isWin:Boolean;
		private var isSame:Boolean;
		
		private var kezi;
		private var sgetWinnzi;
		private var jiang:uint;
		private var tipItem:uint;
		
		public static var Instance:MahJongDice = new MahJongDice();
		
		public function MahJongDice() {
			
		}
		
		//private function init() {
			//11 222 333 , 11 222 123 , 11 123 456
			// [ 1- 6]  一点 ～ 六点 的个数
			//range_array = [0,0,0,2,2,2,2,0]; //特例
			//range_array = [1,1,1,2,0,3,0,0];
			//pai_length = range_array.length;
			//trace(getWin(range_array));
			//[1,1,2,1,0,3]
			
			//var paiArray = MahJongDice.getRandomArray();
			//var paiArray = [1,2,3,1,3,6,6,6];
			//setJoinPaiArray(paiArray);
			
		//}
		
		
		/*
		 * 接收外部传递的游戏数组Array
		 * 
		 * */
		public function setJoinPaiArray(paiArray:Array):void {
			this.pai_array = paiArray;
			this.pai_length = paiArray.length;
			
			rangePaiArray(pai_array);
		}
		
		private function rangePaiArray(paiArray:Array):void {
			trace(":::::::::MahJongDice 类:::::::::  paiArray = " + paiArray);
			this.range_array = this.isEqual(paiArray);
			trace(":::::::::MahJongDice 类:::::::::  range_array = " + range_array);
			
			this.isWin = this.getWin(range_array);
			
			trace(":::::::::MahJongDice 类:::::::::  胡牌 :" + this.isWin);
		}
		
		public function get hasWin():Boolean {
			return this.isWin;
		}
		
		/*
		 *处理一个数组进行归类，返回归类后的牌数组
		 * 
		*/
		public function getRangePaiArray(paiArray:Array):Array {
			var newRangeArray:Array = this.isEqual(paiArray);
			return newRangeArray;
		}
		
		
		//存在的类型
		public function getTing():void {
			
		}
		
		//存在的类型： 11 222 333 , 11 222 123 , 11 123 456
		private function getWin(paiArray:Array):Boolean {
			
			var _resultString:Boolean=true;
				for (var i:int=0; i < paiArray.length; i++) {
						if (paiArray[i] != 0) {
							if (paiArray[i] >= 3) {//算是刻子
								paiArray[i] -=3;
								//trace(":::::::::MahJongDice 类:::::::::  刻子+++++++++++++++++++++++++++++++++++");
								
								if (getWin(paiArray)) {
									return true
								}else {
									paiArray[i] +=3;
								}
							} 
							
							if (!jiang && paiArray[i] >= 2) {
								
								jiang = 1;
								paiArray[i] -=2 ;
								
								//trace(":::::::::MahJongDice 类:::::::::  将+++++++++++++++++++++++++++++++++++");
								
								if (getWin(paiArray)) {
									return true
								}else {
									
									paiArray[i] += 2 ;
									jiang = 0;
								}
								
							} 
							if (paiArray[i] >= 1 && paiArray[i + 1] >= 1 && paiArray[i + 2] >= 1) {//顺子
								//下面情况时：range_array = [0,0,0,2,2,2,2,0];
								paiArray[i] -= 1;
								paiArray[i + 1] -= 1;
								paiArray[i + 2] -= 1;
								
								//trace(":::::::::MahJongDice 类:::::::::  顺子+++++++++++++++++++++++++++++++++++");
								
								if (getWin(paiArray)) {
									return true
								}else {
									paiArray[i] += 1;
									paiArray[i + 1] += 1;
									paiArray[i + 2] += 1;
								}
							}else {
								
								if (this.tipItem == 0 ) {
									this.tipItem = i;
								}	
								//trace(":::::::::MahJongDice 类:::::::::  提示 :" + this.getTipItem());
								_resultString = false; //这里不能返回，
								//"不存在 [万 条 饼 风] 中的一个系列"
								
								
							}
						}	
			}
			
			if (jiang < 1) {
				//trace(":::::::::MahJongDice 类:::::::::  必需要有一个将 jiang");
				_resultString = false;
			}
			return _resultString;
			
		}
		//这里可以是返回一个数组,暂时返回一个项
		public function getTipItem():Number{
			var Item_array:Array = [1, 2, 3, 4, 5, 6];
			return Item_array[this.tipItem];
		}
		
		/*
		 * 
		 *判断可能存在所有相同的情况 ,只能针对于一维数组，不是二维
		 * 
		 * 作用:提取出一个数组,转换成一个类型数组,比如:[1,2,3,4,4,6,6,6]  --->  [1,1,1,2,0,3]
		 * 
		 * var old_array = [1,2,3,4,4,6,6,6]
		 * 
		 * var new_array = this.isEqual(old_array);
		*/
		
		private function isEqual(InArray:Array):Array{
			var result_array:Array;
			var return_array:Array = new Array();
			for (var i = 1; i <= 6; i++) {//只有从一点到六点
				result_array = ArrayUtilities.findMatchIndices(InArray, i);
				//trace("比较相同数的个数 ：" + result_array.length);
				return_array.push(result_array.length);
			}
			return return_array;
		}
		
		
		
		
		/*
		 * 
		 * 产生一个随机数
		*/
		public static function getRandomItem():Number {
			var _random:uint = Math.ceil(Math.random()*6);
			//trace("random = " + random);
			return _random;
		}
		
		/*
		 * 
		 * 产生一个随机数组
		*/
		public static function getRandomArray():Array{
			var test_array:Array = new Array();
			var default_array:Array = new Array();
			var _length:uint = 7;
			for (var i = 0; i < _length; i++) {
				test_array.push(getRandomItem());
			}
			
			return test_array;
			
		}
		
		
		
	}	
}